package cn.itsource.pet.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.GetUserInfoUtil;
import cn.itsource.basic.util.PageList;
import cn.itsource.pet.domain.SearchMasterMessage;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.mapper.SearchMasterMessageMapper;
import cn.itsource.pet.mapper.SearchMasterMsgMapper;
import cn.itsource.pet.query.SearchMasterMessageQuery;
import cn.itsource.pet.service.ISearchMasterMessageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 万能键 alt + enter
 */
@Service //交给spring管理
public class SearchMasterMessageServiceImpl extends BaseServiceImpl<SearchMasterMessage, SearchMasterMessageQuery> implements ISearchMasterMessageService {

    /**
     * 注入mapper层对象
     */
    @Autowired
    private SearchMasterMessageMapper searchMasterMessageMapper;

    /**
     * 注入mapper层对象
     */
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Override
    public PageList<SearchMasterMessage> findPage(SearchMasterMessageQuery q) {

        //分页查询
        //1.分页工具包
        PageHelper.startPage(q.getPage(), q.getSize());

        //2.调用方法
        List<SearchMasterMessage> ts = searchMasterMessageMapper.findPage(q);

        //3.封装一个pageInfo对象
        PageInfo<SearchMasterMessage> pageInfo = new PageInfo<>(ts);

        //4.封装自定义的分页对象
        PageList<SearchMasterMessage> pageList = new PageList<>(pageInfo.getTotal(), pageInfo.getList());

        return pageList;
    }



    @Override
    public AjaxResult publish(SearchMasterMessage masterMessage) {

        //1.通过工具类获取userId
        Long userId = GetUserInfoUtil.getUserId();
        //1.1设置用户id的值
        masterMessage.setUserId(userId);

        //2.入库
        searchMasterMessageMapper.insertSelective(masterMessage);

        return AjaxResult.createSuccess("发布寻主需求成功");
    }

    @Override
    public AjaxResult shenghe(SearchMasterMessage masterMessage) {
        //0.查询审核对象
        SearchMasterMessage dbMasterMessage = searchMasterMessageMapper.selectByPrimaryKey(masterMessage.getId());

        if (dbMasterMessage.getState() != 0){
            return AjaxResult.createSuccess("该数据不能继续审核了");
        }


        //1.如果是驳回,直接修改状态就行了
        Integer state = masterMessage.getState();
        if (state == 2){
            dbMasterMessage.setState(2);
            searchMasterMessageMapper.updateByPrimaryKeySelective(dbMasterMessage);
            return AjaxResult.createSuccess("驳回成功");
        }

        //2.如果是审核通过,查询到审核的数据

        //2.1修改状态
        dbMasterMessage.setState(1);
        searchMasterMessageMapper.updateByPrimaryKeySelective(dbMasterMessage);

        //2.2把数据保存到接单表里面
        SearchMasterMsg searchMasterMsg = BeanUtil.copyProperties(dbMasterMessage, SearchMasterMsg.class);
        searchMasterMsg.init(masterMessage.getNote());

        //3.入库
        searchMasterMsgMapper.insertSelective(searchMasterMsg);

        return AjaxResult.createSuccess("审核成功");
    }
}
